

<template>
  <div class="log">
    <basic-container>
      <avue-crud
        :page.sync="page"
        :permission="permissionList"
         v-bind="bindVal"
         v-on="onEvent"
        >
        <template slot="menuLeft">
          <el-button type="primary" size="small"
		      v-permission="permissionList.setExpireBtn"
		    @click="handleLogExpireSet">日志保存期限设置</el-button>
          <el-button type="primary" size="small"
		      v-permission="permissionList.fieldBtn"
			@click="handleFiledLog">归 档</el-button>
        </template>
      </avue-crud>
      <el-dialog :visible.sync="logSetVisible" title="日志保存期限设置">
        <avue-form
          v-model="logSetForm"
          :option="formOption"
          @submit="submitLogSet">
          <template slot="menuForm">
            <el-button size="small" @click="logSetVisible=false">取消</el-button>
          </template>
        </avue-form>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { delObj, fetchList, getLogExpire, setLogExpire, setFiledLog } from '@/api/admin/log'
import { tableOption } from '@/const/crud/admin/log'
import { mapGetters } from 'vuex'
import baseCrud from '@/mixins/base-crud' 

export default {
  name: 'Log',
  mixins: [baseCrud('admin/log')],
  data() {
    return {
      logSetVisible: false,
      logSetForm: {},
      formOption: {
        submitText: '确认',
        emptyBtn: false,
        column: [{
          label: '周期',
          prop: 'period',
          type: 'select',
          span: 24,
          dicData: [{
            label: '年',
            value: 31536000
          }, {
            label: '季',
            value: 7776000
          }, {
            label: '月',
            value: 2592000
          }, {
            label: '周',
            value: 604800
          }, {
            label: '日',
            value: 86400
          }, {
            label: '小时',
            value: 3600
          }, {
            label: '分',
            value: 60
          }],
          clearable: false
        }, {
          label: '周期值',
          prop: 'value',
          type: 'number',
          span: 24,
          rules: [{
            required: true,
            message: "请输入周期值",
            trigger: "blur"
          },{
            type: 'integer',
            min: 1,
            message: '周期值必须大于0'
          }]
        }]
      }
    }
  },
  created() {
  },
  mounted: function() {
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        fieldBtn: this.vaildData(this.permissions.sys_field_log, false),
		    setExpireBtn: this.vaildData(this.permissions.sys_log_set_expire, false)
      }
    }
  },
  methods: {

    handleLogExpireSet(){
      getLogExpire().then(response => {
        var timeSecond = response.data.data
        for(var d of this.formOption.column[0].dicData){
          if(timeSecond >= d.value && timeSecond % d.value == 0){
            this.logSetForm.period = d.value
            this.logSetForm.value = timeSecond/d.value
            break
          }
        }
        this.logSetVisible = true
      })
    },
    submitLogSet(form, done){
      var timeSecond = form.period * form.value
      setLogExpire(timeSecond).then(() => {
        done()
        this.logSetVisible = false
      })
    },
    handleFiledLog(){
      var tableSelect = this.$refs.crud.tableSelect
      if(tableSelect.length == 0){
        this.$message.info('请选择要归档的记录')
        return
      }
      var ids = tableSelect.map(row => row.id)
      setFiledLog(ids).then(response => {
        this.$message.success('归档成功')
        this.getList(this.page)
      })
     }
  }
}
</script>

<style lang="scss" scoped>
</style>
